package com.uk;

import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.User;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.terminal.UserError;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

public class LoginWindow extends Window {

	private TextField username;
	private TextField password;
	

	private IFaturaBean faturaBean;
	
	public LoginWindow(String caption, IFaturaBean faturaBean){
		super(caption);
		this.faturaBean = faturaBean;
		this.init();
	}
	
	private void init(){
		
		
		CustomLayout vl = new CustomLayout("loginWindowLayout");
		
		
        // Username
        username = new TextField();
        username.setRequired(true);
        username.setValidationVisible(true);
     
        
        vl.addComponent(username, "username");

        // Password
        password = new TextField();
        password.setSecret(true);
        vl.addComponent(password, "password");

        // Login button
        Button loginButton = new Button("Login", new Button.ClickListener() {
            // inline click listener
            public void buttonClick(ClickEvent event) {
            	/*if ("".equals(username.getValue())){
            		username.setComponentError(new UserError("Perdoruesi nuk mund te jete bosh !"));
            		return;
            	}*/
            	//else username.setComponentError(null);
            	
            	User user = faturaBean.login(username.getValue().toString(), password.getValue().toString());
            	if(user == null)
            		getWindow().showNotification("Perdoruesi nuk u autentifikua !");
            	else{
            		((FaturaUKApplication)getWindow().getApplication()).setLoggedUser(user);
            		((FaturaUKApplication)getWindow().getApplication()).loadMainLayout();
            		
            	}
            		
            		
            	
            	
                
            }
        });
        loginButton.setClickShortcut(KeyCode.ENTER);
        vl.addComponent(loginButton, "okbutton");
        this.addComponent(vl);
	}
}
